package com.fastfly.spring.mvc.controller;

import java.security.Principal;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.context.request.WebRequest;

import com.fastfly.domain.Customer;
import com.fastfly.domain.Reservation;
import com.fastfly.domain.Seat;

@Controller
@SessionAttributes( {"reservation", "customer"} )
public class MyAccountController extends BaseController {

	@RequestMapping(method = RequestMethod.GET)
	public void index(Principal currentUser, Model model, WebRequest request) {
		setupCustomer(request);
		log.trace("index");
	}

	@RequestMapping(method = RequestMethod.GET)
	public void milesResult(WebRequest request) {
		setupCustomer(request);
		log.trace("milesResult");
	}

	@RequestMapping(method = RequestMethod.GET)
	public void reservations(WebRequest request) {
		log.trace("reservations");
		setupCustomer(request);
	}

	@RequestMapping(method = RequestMethod.GET)
	public String cancelFlight(@RequestParam("reservationId") long reservationId, Model model, WebRequest request) {
		log.trace("cancelFlight");
		String view = "myaccount/reservations";

		flightService.cancelReservation(reservationId);

		setupCustomer(request);
		return view;
	}

	@RequestMapping(method = RequestMethod.GET)
	public void reserveSeats(@RequestParam("reservationId") long reservationId, Model model, WebRequest request) {
		setupCustomer(request);

		log.trace("reserveSeats reservationId: " + reservationId);
		Reservation reservation = (Reservation) customerService.findById(reservationId, Reservation.class);

		request.setAttribute("flightDeparting", reservation.getDepartureFlight(), WebRequest.SCOPE_REQUEST);
		request.setAttribute("flightReturning", reservation.getReturnFlight(), WebRequest.SCOPE_REQUEST);

		List<Seat> list = flightService.findAvailableSeatInFlight(reservation.getDepartureFlight().getId());
		request.setAttribute("departingSeatList", list, WebRequest.SCOPE_REQUEST);

		list = flightService.findAvailableSeatInFlight(reservation.getReturnFlight().getId());
		request.setAttribute("returningSeatList", list, WebRequest.SCOPE_REQUEST);

		log.trace("reservation.getTicketCollection().size(): " + reservation.getTicketCollection().size());
		model.addAttribute("reservation", reservation);
	}

	@RequestMapping(method = RequestMethod.POST)
	public void changeSeats(@ModelAttribute Reservation reservation, BindingResult result, Model model, WebRequest request) {
		log.trace("changeSeats reservation.getTicketCollection().size(): " + reservation.getTicketCollection().size());
		reservation = flightService.changeSeats(reservation);
	}

	@RequestMapping(method = RequestMethod.GET)
	public void searchCustomer(WebRequest request) {
		log.trace("searchCustomer");
		setupCustomer(request);
	}

	@RequestMapping(method = RequestMethod.GET)
	public void updatePersonal(WebRequest request, Model model) {
		log.trace("updatePersonal");
		Customer customer = getCurrentCustomer(request);
		log.debug("customer: " + customer);
		model.addAttribute("customer", customer);

	}

	@RequestMapping(method = RequestMethod.POST)
	public String submitPersonal(@ModelAttribute Customer customer, BindingResult result, Model model, WebRequest request, SessionStatus status) {
		log.debug("submitPersonal customer: " + customer);
		String view = "myaccount/updateSuccess";		
		customer.validatePassword(result);
		if (result.hasErrors()) {
			view = "myaccount/updatePersonal";
		} else {
			customerService.merge(customer);
			status.setComplete();			
		}
		return view;
	}

}
